package com.gzgy.ustore.api.web.controller.webservice;

import com.gzgy.ustore.api.common.util.DateUtil;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.service.PdaMgrService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import com.ustore.model.PdaMgr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;

@Controller
public class PdaMgrController extends BaseController {

    @Autowired
    private PdaMgrService pdaMgrService;

    @RequestMapping(value = "registerDevice")
    public String registerDevice(ModelMap modelMap, String userId, String session, PdaMgr pdaMgr){

        Assert.hasText(pdaMgr.getDvcGroupId(),"dvcGroupId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(pdaMgr.getDvcType(),"dvcType " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(pdaMgr.getOrgTreeId(),"orgTreeId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(pdaMgr.getOrgName(),"orgName " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(pdaMgr.getCompanyId()+"","companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(pdaMgr.getDvcId(),"dvcId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.notNull(pdaMgr.getStatus(),"status " + I18nUtil.getMessage(-2,null,"zh_CN"));

        pdaMgr.setCreatedTime(DateUtil.getCurrentDate());
        pdaMgr.setCreatedBy(userId);

        modelMap.put("message", ResultUtil.getResultJson(pdaMgrService.registerPda(pdaMgr)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "updateDeviceInfo",method = RequestMethod.POST)
    public String updateDeviceInfo(ModelMap modelMap,String userId,String session ,PdaMgr pdaMgr,String flag) throws SQLException {

        Assert.hasText(flag , "flag "+I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.notNull(pdaMgr.getId(),"id "+I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.notNull(pdaMgr.getStatus(),"status " + I18nUtil.getMessage(-2,null,"zh_CN"));
        if("0".equals(flag)){
            Assert.hasText(pdaMgr.getDvcGroupId(),"dvcGroupId " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(pdaMgr.getDvcType(),"dvcType " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(pdaMgr.getOrgTreeId(),"orgTreeId " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(pdaMgr.getOrgName(),"orgName " + I18nUtil.getMessage(-2,null,"zh_CN"));

        }
        pdaMgr.setCreatedTime(DateUtil.getCurrentDate());
        pdaMgr.setCreatedBy(userId);

        modelMap.put("message", ResultUtil.getResultJson(pdaMgrService.updateDeviceInfo(pdaMgr,flag)));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "dvcList",method = RequestMethod.POST)
    public String pdaList(ModelMap modelMap,String userId,String session,String companyId,String orgId,String dvcGroupId,String index,String page){

        Assert.hasText(companyId , "companyId "+I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(orgId , "orgId "+I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(index , "index "+I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(page , "page "+I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message",ResultUtil.getResultJson(pdaMgrService.pdaList(companyId,orgId,dvcGroupId,index,page)));

        return SHOW_JSON_NAME;
    }

}
